export class EntityHelper
{
    public static  getWithdrawObject2(room:Room,creep:Creep,count:number)
    {
        const core  = room.getEntity("CoreEntity");
        if(core  )
        {
            const stores = core.getTakeStores({resourceType:RESOURCE_ENERGY,amount:count});
            if(stores.length>0)
            {
                
                return stores[0].id
            }
        }
        if(room.energyCapacityAvailable == room.energyAvailable)
        {
            const containers = room.getStructures(STRUCTURE_CONTAINER);
            const minobj = _.min(containers.filter(e=>e.store.getUsedCapacity(RESOURCE_ENERGY)>count),e=>e.pos.distance(creep.pos))
            if(minobj)
            {
                return minobj.id
            }
            return false
        }
        else 
        {
            const containers = room.getEntity("SpawnEntity").getContainer().map(e=>Game.getObjectById(e)!);
            const minobj = _.min(containers.filter(e=>e.store.getUsedCapacity(RESOURCE_ENERGY)>count),e=>e.pos.distance(creep.pos))
            if(minobj)
            {
                return minobj.id
            }
            return false
        }
        
        
    }
    public static  getWithdrawObject(room:Room,count:number)
    {
        const core  = room.getEntity("CoreEntity");
        if(core  )
        {
            const stores = core.getTakeStores({resourceType:RESOURCE_ENERGY,amount:count});
            if(stores.length>0)
            {
                
                return stores[0].id
            }
        }

        const spawnentity = room.getEntity("SpawnEntity");
        const containers = spawnentity.getContainer();
        const getObject=(ids:Id<AnyStoreStructure>[],num:number)=>
        {
            const container = ids.filter(e=>
            {
                    const container = Game.getObjectById(e);
                    if(container&&container.store.getUsedCapacity(RESOURCE_ENERGY)>=num) 
                    {
                        return true;
                    }
                    return false;
                })
                
            if(container.length>0)
            {
                return container[0];
            }
            return false;
        }
        if(containers && containers.length>0)
        {
            let withdrawobject = getObject(containers,count);
            if(withdrawobject)
            {
                return withdrawobject;
            }
        }

        return false
        
    }
    public  static getTransferAnyResourceObject(room:Room,count:number)
    {
        const core  = room.getEntity("CoreEntity");
        const stores = core.getStores({resourceType:RESOURCE_ENERGY,amount:count});
        if(stores.length>0)
        {
            
            return stores[0]
        }
        return undefined
    }
    public static  getTransferObject2(room:Room,count:number)
    {
        /**
        // 分3种情况
        // 1.spawn 没有link
        // 2.spawn 有link
        // 3.core 有link
         */ 
        const spawnentity = room.getEntity("SpawnEntity");
        const core  = room.getEntity("CoreEntity");
        const controller = room.controller!;
        if(spawnentity.Link)
        {
            if(!core.Link)
            {
                // 中间等级
                {
                    // 有LINK的时候。只有container空了才填充
                    const containers = _.sortBy( 
                        (spawnentity.getContainer().map(e=>Game.getObjectById(e)) as StructureContainer[]).filter(e=>e),e=>e.store.getFreeCapacity()==0);
                    if(containers.length>0)
                    {
                        // spawn 有container 之后 则controller才有可能有container
                        for(const e of containers)
                        {
                            if(e.store.getUsedCapacity(RESOURCE_ENERGY)==0)
                            {
                                return e
                            }
                        }                     
                    }
                }
            }
            // 有Link表示已经5级了。则一定有仓库
            // 如果controller 
             
            {
                // 都有link就只运到core
                // 高等级阶段了
                // 仓库的资源足够了。则判断一下控制器
                if(core.ManagerPosition.distance(controller.pos)<5)
                {
                    // 新的布局
                    const stores = core.getStores({resourceType:RESOURCE_ENERGY,amount:count});
                    if(stores.length>0)
                    {
                        return stores[0]
                    }
                }
                else
                {
                    if(core.resourceCount(RESOURCE_ENERGY)>130000)
                    {
                        const stores = controller.getContainer();
                        if(stores.length>0&& stores[0].store.getFreeCapacity(RESOURCE_ENERGY)>=count)
                        {
                            
                            return stores[0]
                        }
                    }
                    // 控制器放不下。则放到仓库
                    const stores = core.getStores({resourceType:RESOURCE_ENERGY,amount:count});
                    if(stores.length>0)
                    {
                        
                        return stores[0]
                    }
                }
            }
        }
        else 
        {
            // 没有LInk则处于低级阶段
            // 填充目标
            /**
             * spawn 
             * spawn container
             * controller containr
            */
            // 给spawn
            const containers = _.sortBy( 
                (spawnentity.getContainer().map(e=>Game.getObjectById(e)) as StructureContainer[]).filter(e=>e),e=>e.store.getFreeCapacity()==0);
            if(containers.length>0)
            {
                // spawn 有container 之后 则controller才有可能有container
                for(const e of containers)
                {
                    if(e.store.getFreeCapacity(RESOURCE_ENERGY)>=count)
                    {
                        return e
                    }
                }
                //

                // 然后再给container
                const stores = controller.getContainer();
                if(stores.length>0&& stores[0].store.getFreeCapacity(RESOURCE_ENERGY)>=count)
                {
                    
                    return stores[0]
                }
            }
            else 
            {
                // 没有container 则 直接给spawn
                const spawn = spawnentity.spawnStructures();
                if(spawn.length>0 )
                {
                    if(spawn[0].store.getFreeCapacity(RESOURCE_ENERGY)>0)
                    {
                        return spawn[0]
                    }
                }
            }
            if(core.Storage && core.Storage.store.getFreeCapacity()>=count)
            {
                return core.Storage;
            }
        }
        return undefined;
    }
    public static  getTransferObject(room:Room,count:number)
    {
        /**
        // 分3种情况
        // 1.spawn 没有link
        // 2.spawn 有link
        // 3.core 有link
         */ 
        const getControllerStore=(inroom:Room,incount:number)=>
        {
            // 3.再次之给 控制器
            const controller  = inroom.controller!;
            if(controller  )
            {
                const stores = controller.getContainer();
                if(stores.length>0&& stores[0].store.getFreeCapacity(RESOURCE_ENERGY)>=incount)
                {
                    
                    return stores[0]
                }
            }
            const core  = inroom.getEntity("CoreEntity");
            const container = core.Container;
            if(container  && container.store.getFreeCapacity(RESOURCE_ENERGY)>=incount)
                {
                    
                    return container
                }
            return undefined
        }
        // 2.其次给spawn
        // 运
        const spawnentity = room.getEntity("SpawnEntity");
        const link = spawnentity.Link;
        if(!link || link.store.getUsedCapacity(RESOURCE_ENERGY)==0)
        {
            // 空的container
            const containers = _.sortBy( 
                (spawnentity.getContainer().map(e=>Game.getObjectById(e)) as StructureContainer[]).filter(e=>e),e=>e.store.getFreeCapacity()==0);
            if(containers.length>0)
            {
                for(const e of containers)
                {
                     
                    if(e.store.getFreeCapacity(RESOURCE_ENERGY)>=count)
                    {
        
                        return e
                    }
                 
                }
            }
        }
       
        
        const core  = room.getEntity("CoreEntity");
        if(core  )
        {
            if(core.resourceCount(RESOURCE_ENERGY)>130000)
            {
                // 仓库有足够的资源后。先扔到 控制器
                let controllerStore =  getControllerStore(room,count);
                if(controllerStore)
                {
                    return controllerStore
                }
            }
            const stores = core.getStores({resourceType:RESOURCE_ENERGY,amount:count});
            if(stores.length>0)
            {
                
                return stores[0]
            }
        }

        
        // 没有container则直接给spawn,spawn特殊处理
        const spawn = spawnentity.spawnStructures();
        if(spawn.length>0 )
        {
            if(spawn[0].store.getFreeCapacity(RESOURCE_ENERGY)>0)
            {
                return spawn[0]
            }
        }

        return getControllerStore(room,count);
    }
   
}